package leetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class LogSystem {

//	TreeMap<String, Integer> treeMapS=new TreeMap<>();
//	TreeMap<String, ArrayList<Integer>> treeMapM=new TreeMap<>();
//	TreeMap<String, ArrayList<Integer>> treeMapH=new TreeMap<>();
//	TreeMap<String, ArrayList<Integer>> treeMapD=new TreeMap<>();
//	TreeMap<String, ArrayList<Integer>> treeMapMo=new TreeMap<>();
//	TreeMap<String, ArrayList<Integer>> treeMapY=new TreeMap<>();
	TreeMap<String, ArrayList<Integer>>[] maps=new TreeMap[6];
	int[] endPos;
	HashMap<String, Integer> graMap;
    public LogSystem() {
    	for (int i = 0; i < 6; i++) {
    		maps[i]=new TreeMap<String, ArrayList<Integer>>();
		}
    	endPos=new int[6];
    	endPos[0]=4;
    	for (int i = 1; i < endPos.length; i++) {
			endPos[i]=endPos[i-1]+3;
		}
    	
    	graMap=new HashMap<>();
    	graMap.put("Year", 0);
    	graMap.put("Month", 1);
    	graMap.put("Day", 2);
    	graMap.put("Hour", 3);
    	graMap.put("Minute", 4);
    	graMap.put("Second", 5);
    	
    }
    
    public void put(int id, String timestamp) {
//    	treeMapS.put(timestamp, id);
//    	treeMapD.put(timestamp.substring(0, 16), value)
    	int i=0;
    	for (TreeMap<String, ArrayList<Integer>> treeMap : maps) {
    		String keyStr=timestamp.substring(0, endPos[i]);
			if(treeMap.containsKey(keyStr)){
				(treeMap.get(keyStr)).add(id);
			}else {
				ArrayList<Integer> list=new ArrayList<>();
				list.add(id);
				treeMap.put(keyStr, list);
			}
			i++;
		}
    }
    
    public List<Integer> retrieve(String s, String e, String gra) {
    	List<Integer> result=new ArrayList<>();
    	int index=graMap.get(gra);
    	s=s.substring(0, endPos[index]);
    	e=e.substring(0, endPos[index]);
    	TreeMap<String, ArrayList<Integer>> treeMap=maps[index];
    	SortedMap<String,ArrayList<Integer>> subMap=treeMap.subMap(s,true, e,true);
    	for (ArrayList<Integer> integers : subMap.values()) {
			for (Integer integer : integers) {
				result.add(integer);
			}
		}
    	return result;
    }
    
    public static void main(String[] args) {
		LogSystem logSystem=new LogSystem();
		logSystem.put(1, "2017:01:01:23:59:59");
		logSystem.put(2, "2017:01:01:22:59:59");
		logSystem.put(3, "2016:01:01:00:00:00");
		System.out.println(logSystem.retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"));
		System.out.println(logSystem.retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"));
	}
}
